import 'package:aitao_flutter/common_ui/confirm_btn.dart';
import 'package:aitao_flutter/res/r.dart';
import 'package:aitao_flutter/style/box_style.dart';
import 'package:aitao_flutter/style/color_style.dart';
import 'package:aitao_flutter/style/edgeInsets_style.dart';
import 'package:aitao_flutter/style/text_style.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get_state_manager/src/simple/get_state.dart';
import '../../const/common_const.dart';
import '../../style/radius_style.dart';
import '../controller/code_login_controller.dart';

class CodeLoginPage extends StatefulWidget {
  const CodeLoginPage({Key? key}) : super(key: key);

  @override
  State<CodeLoginPage> createState() => _CodeLoginPageState();
}

class _CodeLoginPageState extends State<CodeLoginPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: ColorStyle.themeColor,
        body: GestureDetector(
            behavior: HitTestBehavior.translucent,
            onTap: () {
              // 点击空白区域时收起键盘
              FocusScope.of(context).unfocus();
            },
            child: GetBuilder<CodeLoginController>(
              init: CodeLoginController(),
              builder: (controller) {
                return CustomScrollView(slivers: [
                  // 输入框
                  inputWidget(controller),
                  loginBtnWidget(controller),
                  agreementWidget(controller)
                ]);
              },
            )));
  }

  Widget inputWidget(CodeLoginController controller) {
    return SliverToBoxAdapter(
        child: Container(
            decoration: BoxDecoration(
              borderRadius: RadiusStyle.radiusAll(14),
              color: ColorStyle.whiteColor,
            ),
            margin: EdgeInsetsStyle.customMargin(20, 110, 20, 0),
            padding: EdgeInsetsStyle.customMargin(30, 50, 20, 14),
            child: Column(
              children: [
                // 手机号
                Container(
                  height: 44.h,
                  padding: EdgeInsetsStyle.edgeInsets(20, 0),
                  decoration: BoxDecoration(
                      borderRadius: RadiusStyle.radiusAll(25),
                      color: const Color(0xFFF5F7F7)),
                  child: Row(children: [
                    Expanded(
                        child: TextField(
                      controller: controller.phoneController,
                      keyboardType: TextInputType.number,
                      textInputAction: TextInputAction.next,
                      decoration: const InputDecoration(
                        hintText: LoginStringConst.inputPhoneHintText,
                        border: InputBorder.none,
                      ),
                    ))
                  ]),
                ),
                Box.vBox10,
                // 上次登录
                Text(
                  controller.lastPhone == ''
                      ? controller.lastPhone
                      : '上次登录手机号${controller.lastPhone}',
                  style: Styles.normalText_12,
                ),
                Box.vBox14,
                // 验证码
                Container(
                  height: 44.h,
                  padding: EdgeInsetsStyle.edgeInsets(20, 0),
                  decoration: BoxDecoration(
                      borderRadius: RadiusStyle.radiusAll(25),
                      color: const Color(0xFFF5F7F7)),
                  child: Row(children: [
                    Expanded(
                        child: TextField(
                      controller: controller.codeController,
                      keyboardType: TextInputType.number,
                      textInputAction: TextInputAction.next,
                      decoration: const InputDecoration(
                        hintText: LoginStringConst.inputVerCodeText,
                        border: InputBorder.none,
                      ),
                    )),
                    GestureDetector(
                      child: Text(
                          controller.isCountingDown == true
                              ? '${controller.countdownSeconds}秒后重新获取'
                              : LoginStringConst.getVerCodeText,
                          style: controller.isCountingDown == false
                              ? Styles.blueText_12
                              : Styles.mainText_12),
                      onTap: () {
                        controller.getSmsCode();
                      },
                    )
                  ]),
                ),
                Box.vBox40,
                Align(
                  alignment: Alignment.centerRight,
                  child: GestureDetector(
                    child: Text(
                      LoginStringConst.passWordLoginText,
                      style: Styles.normalText_14,
                    ),
                    onTap: () {
                      controller.passwordLoginClick();
                    },
                  ),
                )
              ],
            )));
  }

  Widget loginBtnWidget(CodeLoginController controller) {
    return SliverToBoxAdapter(
        child: Container(
            margin: EdgeInsetsStyle.customMargin(20, 110, 20, 0),
            child: ConfirmButton(
              title: LoginStringConst.loginText,
              callBack: () {
                controller.loginClick();
              },
            )));
  }

  Widget agreementWidget(CodeLoginController controller) {
    return SliverToBoxAdapter(
        child: Container(
      alignment: Alignment.center,
      decoration: BoxDecoration(
        borderRadius: RadiusStyle.radiusAll(14),
        color: ColorStyle.themeColor,
      ),
      margin: EdgeInsetsStyle.customMargin(20, 40, 20, 0),
      // padding: EdgeInsetsStyle.customMargin(30, 50, 20, 14),
      child: Column(
        children: [
          Text.rich(TextSpan(children: [
            TextSpan(
              text: "登录即同意",
              style: Styles.whiteText_12,
            ),
            TextSpan(
                text: LoginStringConst.userAgreementText,
                style: Styles.blueText_12),
          ])),
          Box.vBox20,
          GestureDetector(
            child: Image.asset(ImagePath.assetsImagesWXLogo),
            onTap: () {
              controller.wechatLoginClick();
            },
          )
        ],
      ),
    ));
  }
}
